//
//  VisitorView.swift
//  SwiftSina
//
//  Created by 刘战军 on 16/5/14.
//  Copyright © 2016年 LiuZhanJun. All rights reserved.
//





import UIKit
import SnapKit

/**
*  访客视图的协议
*/
protocol VisitorViewDelegate: NSObjectProtocol {
    
    /// 访客视图注册
    func visitorViewDidRegister()
    /// 访客视图登陆
    func visitorViewLogin()
}

class VisitorView: UIView {
    
    // MARK: - 代理属性
    /// 提示: 代理一定不要忘记 weak
    weak var delegate: VisitorViewDelegate?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
        // 添加监听事件
        registerButton.addTarget(self, action: "didClickRegisterButton", forControlEvents: .TouchUpInside)
        loginButton.addTarget(self, action: "didClickLoginButton", forControlEvents: .TouchUpInside)
        
    }
    
    // MARK: - 监听方法
    @objc private func didClickRegisterButton() {
        print("点击注册")
        delegate?.visitorViewDidRegister()
        
    }
    @objc private func didClickLoginButton() {
        print("点击登陆")
        delegate?.visitorViewLogin()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 私有属性
    // 设置标签
    private lazy var iconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    // 遮罩视图
    private lazy var maskIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    // 设置小房子标签
    private lazy var homeIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    // 消息标签
     private lazy var messageLabel: UILabel = UILabel(text: "关注一些人, 回去看看这里有什么惊喜", alignment: .Center, fontSize: 13)
    // 注册按钮
    private lazy var registerButton: UIButton = UIButton(title: "注册", backImageName: "common_button_white_disable")
    // 登录按钮
     private lazy var loginButton: UIButton = UIButton(title: "登陆", backImageName: "common_button_white_disable")
    
}


// MARK: - 设置访客视图
extension VisitorView {
    
    // MARK: - 公共方法
    func setupInfo(imageName: String? = nil, message: String) {
        
        messageLabel.text = message
        
        guard let imageName = imageName else {
            // 开启动画
            startAnimation()
            
            return
        }
        
        // 如果不是首页, 就不需要隐藏房子
        homeIconView.hidden = true
        // homeIconView.hidden = true
        maskIconView.hidden = true
        
        iconView.image = UIImage(named: imageName)
    }
    
    /**
    开始动画
    */
    private func startAnimation() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        // 设置动画效果
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        // 等视图销毁之后才会销毁
        anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    /**
    添加视图, 设置约束
    */
    private func setupUI() {
        
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 去掉系统的自动布局约束
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }

        // 图标视图
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }

        // 小房子视图
        homeIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }

        // 提示文字
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: 224, height: 36))
        }

        // 注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            make.left.equalTo(messageLabel)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        
        // 登陆按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
        
        // 遮罩视图
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.bottom.equalTo(registerButton)
        }
        
        // 空白区域可以使用颜色填充
        backgroundColor = UIColor(white: 227.0 / 225.0, alpha: 1.0)
    }
}